/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tarearonald;

import java.sql.Connection;
import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Oscar Eduardo
 */
public class jInterConsultar extends javax.swing.JInternalFrame implements InternalFrameListener {

    /**
     * Creates new form jInterConsultar
     */
    JMenuItem ref;

    public jInterConsultar(JMenuItem ref) {
        this.ref = ref;
        ref.setEnabled(false);
        this.addInternalFrameListener(this);
        setClosable(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        initComponents();
        llenarTablaVentas();

    }

    public void llenarTablaVentas() {
        DefaultTableModel temp = (DefaultTableModel) jTableVentas.getModel();
        JTable jTable1 = new JTable(temp);
        Object[] fila = new Object[4];
        Conex obj = new Conex();
        Connection con = obj.conectar();
        String query = "select * from ventas";
        String result[][];
        result = obj.select(con, query);

        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < 4; j++) {
                fila[j] = result[i][j];
            }
            temp.addRow(fila);
        }
        obj.close(con);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableVentas = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableDetalles = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableVentas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Venta", "Subtotal", "Iva", "Total"
            }
        ));
        jTableVentas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableVentasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableVentas);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableDetalles.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Producto", "Cantidad", "Precio"
            }
        ));
        jTableDetalles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDetallesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableDetalles);

        jLabel1.setText("Ventas");

        jLabel2.setText("Detalles de venta");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(0, 384, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableDetallesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDetallesMouseClicked
        // TODO add your handling code here:

    }//GEN-LAST:event_jTableDetallesMouseClicked

    private void jTableVentasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableVentasMouseClicked
        // TODO add your handling code here:
        DefaultTableModel temp = (DefaultTableModel) jTableDetalles.getModel();
        JTable jTable1 = new JTable(temp);

        int select = -1;
        select = jTableVentas.getSelectedRow();
        if (select != -1) {
            for (int i = jTableDetalles.getRowCount() - 1; i >= 0; i--) {
                temp.removeRow(i);
            }
            Conex obj = new Conex();
            Connection con = obj.conectar();
            String query = "select productos.nombre, detventa.cantidad, detventa.total"
                    + " from Productos, detventa where detventa.idventa = '"
                    + jTableVentas.getValueAt(select, 0).toString()+ "' "
                    + " and productos.id = detventa.idproducto";
            String result[][];
            result = obj.select(con, query);
            Object[] fila = new Object[3];
            for (int i = 0; i < result.length; i++) {
                for (int j = 0; j < 3; j++) {
                    fila[j] = result[i][j];
                }
                temp.addRow(fila); 
            }

        }

    }//GEN-LAST:event_jTableVentasMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableDetalles;
    private javax.swing.JTable jTableVentas;
    // End of variables declaration//GEN-END:variables

    @Override
    public void internalFrameOpened(InternalFrameEvent e) {

    }

    @Override
    public void internalFrameClosing(InternalFrameEvent e) {
        this.ref.setEnabled(true);
    }

    @Override
    public void internalFrameClosed(InternalFrameEvent e) {

    }

    @Override
    public void internalFrameIconified(InternalFrameEvent e) {


    }

    @Override
    public void internalFrameDeiconified(InternalFrameEvent e) {

    }

    @Override
    public void internalFrameActivated(InternalFrameEvent e) {
    }

    @Override
    public void internalFrameDeactivated(InternalFrameEvent e) {

    }
}
